<template>
  <div>
    <div>
      <h1>订单列表</h1>
    </div>
    <div>
      <el-form :inline="true" :model="searchForm" class="demo-form-inline">
        <el-form-item label="关键词">
          <el-input
            v-model="searchForm.keyWord"
            placeholder="关键词"
            clearable
          />
        </el-form-item>
        <el-form-item label="请选择">
          <el-select
            v-model="searchForm.typeId"
            placeholder="请选择"
            clearable
            style="width: 120px"
          >
            <el-option label="普通订单" :value="1" />
            <el-option label="秒杀订单" :value="2" />
          </el-select>
        </el-form-item>
        <el-form-item label="请选择">
          <el-select
            v-model="searchForm.statusId"
            placeholder="请选择"
            clearable
            style="width: 120px"
          >
            <el-option label="未支付" :value="0" />
            <el-option label="已支付" :value="1" />
            <el-option label="待发货" :value="2" />
          </el-select>
        </el-form-item>

        <el-form-item>
          <el-button type="primary" @click="loadList">搜索</el-button>

          <el-button type="primary" @click="handlerAddOrder"
            >添加订单</el-button
          >
          <el-button type="primary" @click="batchDeleteOrder"
            >批量删除</el-button
          >
          <el-button type="primary" @click="ExcelDeleteOrder">导出</el-button>
        </el-form-item>
      </el-form>
    </div>
    <div>
      <el-table
        :data="tableData"
        stripe
        style="width: 100%"
        @selection-change="deleteSelectionChange"
      >
        <el-table-column type="selection" width="55" />
        <el-table-column prop="orderId" label="ID" width="50" />
        <el-table-column prop="orderNo" label="订单编号" width="100" />
        <el-table-column prop="orderStatus" label="订单状态" width="100">
          <template #default="scope">
            <span v-if="scope.row.orderStatus == 0">未支付</span>
            <span v-if="scope.row.orderStatus == 1">已支付</span>
            <span v-if="scope.row.orderStatus == 2">待发货</span>
          </template>
        </el-table-column>
        <el-table-column prop="orderAmount" label="总金额" width="100" />
        <el-table-column prop="payTime" label="支付时间" width="150" />
        <el-table-column prop="typeName" label="类型" width="120" />
        <el-table-column prop="userId" label="用户" width="100" />
        <el-table-column prop="prodInfo" label="商品名称" width="150" />
        <el-table-column prop="createTime" label="创建时间" width="150" />
        <el-table-column prop="orderImages" label="订单图片" width="180">
          <template #default="scope">
            <el-image
              style="width: 50px; height: 50px"
              v-if="
                scope.row.orderImages != null && scope.row.orderImages != ''
              "
              v-for="item in scope.row.orderImages"
              :key="item"
              :src="item"
            ></el-image>
          </template>
        </el-table-column>
        <el-table-column label="操作"> </el-table-column>
      </el-table>
    </div>
    <div style="margin-top: 20px">
      <div class="demo-pagination-block">
        <el-pagination
          v-model:current-page="searchForm.pageNum"
          v-model:page-size="searchForm.pageSize"
          :page-sizes="[2, 4, 6, 8]"
          :background="true"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </div>
    <div>
      <el-dialog v-model="dialogTableVisible" title="添加订单" width="900">
        <div>
          <el-table :data="gridData" @selection-change="handleSelectionChange">
            <el-table-column type="selection" width="55" />
            <el-table-column prop="infoId" label="ID" width="50" />
            <el-table-column prop="prodSku" label="SKU" width="100" />
            <el-table-column prop="prodId" label="商品ID" width="120" />
            <el-table-column prop="prodName" label="商品名称" width="100" />
            <el-table-column prop="prodPrice" label="商品价格" width="100" />
            <el-table-column prop="prodStock" label="商品库存" width="100" />
            <el-table-column prop="createTime" label="创建时间" width="100" />
            <el-table-column prop="buyNum" label="购买数量" width="100">
              <template #default="scope">
                <el-input v-model="scope.row.buyNum"></el-input>
              </template>
            </el-table-column>
          </el-table>
        </div>
        <div style="margin-top: 20px">
          <div class="demo-pagination-block">
            <el-pagination
              v-model:current-page="searchForm.pageNum"
              v-model:page-size="searchForm.pageSize"
              :page-sizes="[2, 4, 6, 8]"
              :background="true"
              layout="total, sizes, prev, pager, next, jumper"
              :total="totalProd"
              @size-change="handleSizeChangeProd"
              @current-change="handleCurrentChangeProd"
            />
          </div>
          <div>总价:{{ amountCount }}元</div>
        </div>
        <div style="margin-top: 50px">
          <el-button type="primary" @click="onAddOrder()"
            >购买商品生成订单</el-button
          >
        </div>
      </el-dialog>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ElMessage } from "element-plus";
import {
  getOrderPage,
  getprodListPage,
  getProdInfoList,
  getAddOrder,
  deleteBatchOrder,
} from "../api/api";
import { onMounted, reactive, ref } from "vue";

const tableData = ref([]);
const total = ref(0);
const totalProd = ref(0);
const dialogTableVisible = ref(false);
const multipleSelection = ref<any[]>([]);
const deleteSelection = ref<any[]>([]);
const gridData = ref([]);
const amountCount = ref(0);

const searchForm = ref({
  keyWord: "",
  pageNum: 1,
  pageSize: 2,
});

const addForm = ref({
  keyWord: "",
  pageNum: 1,
  pageSize: 2,
});

//导出
const ExcelDeleteOrder = () => {
  let ids = "";
  if (deleteSelection.value.length == 0 || deleteSelection.value == null) {
    ElMessage.error("请选择数据");
    return;
  }

  deleteSelection.value.forEach((item) => {
    if (ids == "") {
      ids = item.orderId;
    } else {
      ids = ids + "," + item.orderId;
    }
  });
  let url = "http://localhost:9011/prod/order/getExpartList/" + ids;
  window.open(url);
};

//批量删除
const batchDeleteOrder = () => {
  if (deleteSelection.value.length == 0 || deleteSelection.value == null) {
    ElMessage.error("请选择数据");
    return;
  }
  deleteBatchOrder(deleteSelection.value).then((res) => {
    if (res.data.code == 200) {
      ElMessage.success("删除成功");
      loadList();
    } else {
      ElMessage.error(res.data.msg);
    }
  });
};

//批量多选
const deleteSelectionChange = (val: any[]) => {
  deleteSelection.value = val;
};

//添加订单
const onAddOrder = () => {
  if (multipleSelection.value.length == 0 || multipleSelection.value == null) {
    ElMessage.error("请选择商品");
    return;
  }
  getAddOrder(multipleSelection.value).then((res) => {
    if (res.data.code == 200) {
      ElMessage.success("添加成功");
      dialogTableVisible.value = false;
      loadList();
    } else {
      ElMessage.error(res.data.msg);
    }
  });
};

//多选
const handleSelectionChange = (val: any[]) => {
  amountCount.value = 0;
  multipleSelection.value = val;
  multipleSelection.value.forEach((item) => {
    console.log("-------------", item.buyNum);

    amountCount.value += item.prodPrice * item.buyNum;
  });
};

//显示添加订单框
const handlerAddOrder = () => {
  dialogTableVisible.value = true;
  getProd();
};

//分页
const handleSizeChange = (val: number) => {
  console.log(`${val} items per page`);
  searchForm.value.pageSize = val;
  loadList();
};
const handleCurrentChange = (val: number) => {
  console.log(`current page: ${val}`);
  searchForm.value.pageNum = val;
  loadList();
};

//商品分页
const handleSizeChangeProd = (val: number) => {
  console.log(`${val} items per page`);
  addForm.value.pageSize = val;
  getProd();
};
const handleCurrentChangeProd = (val: number) => {
  console.log(`current page: ${val}`);
  addForm.value.pageNum = val;
  getProd();
};

//获取商品列表
const getProd = () => {
  getProdInfoList(addForm.value).then((res) => {
    if (res.data.code == 200) {
      gridData.value = res.data.data.records;
      totalProd.value = res.data.data.total;
    }
  });
};

//获取列表
const loadList = () => {
  getOrderPage(searchForm.value).then((res) => {
    if (res.data.code == 200) {
      tableData.value = res.data.data.records;
      total.value = res.data.data.total;
    }
  });
};

//初始化数据
onMounted(() => {
  loadList();
});
</script>

<style scoped></style>
